﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.ServiceModel.Description;

namespace SelfHost
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                args = new string[1];
                args[0] = "http://localhost:1337/";
                try
                {
                    if (Directory.Exists("C:\\modules") == false) { Directory.CreateDirectory("C:\\modules"); }
                    foreach (string file in Directory.EnumerateFiles(".", "*.dll")) { File.Copy(file, "C:\\modules\\" + file, true); }
                }
                catch { }
            }
            if (args.Length < 1) { return; }
            Uri baseAddr = null;
            try
            {
                baseAddr = new Uri(args[0]);
            }
            catch (Exception x)
            {
                Console.WriteLine(x.Message);
                return;
            }
            while (true)
            {
                try
                {
                    using (ServiceHost host = new ServiceHost(typeof(ArchitectureV2.SocialLearningService), baseAddr))
                    {
                        host.Open();
                        while (true) { Thread.Sleep(Timeout.Infinite); }
                    }
                }
                catch (Exception x) { Console.WriteLine(x.Message); }
                Thread.Sleep(3000);
            }
        }
    }
}
